package com.example.nfc.tools;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.view.Gravity;

import com.example.nfc.dialog.AlertDialog;
import com.example.nfc.dialog.BaseDialog;
import com.example.nfc.server.utils.NToast;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.List;

/**
 * Create by 游德禄 on 2020-4-23
 */
public class CheckPermissionUtil {
    public static void check(final Activity activity, String[] perimission, final AfterDo afterDo){
        AndPermission.with(activity)
                .permission(
                      perimission
                )
                .onGranted(new Action() {
                    @Override
                    public void onAction(List<String> permissions) {
                        afterDo.doIt();
                    }
                })
                .onDenied(new Action() {
                    @Override
                    public void onAction(List<String> permissions) {
                        boolean io = false ;
                        boolean camera = false ;
                        boolean tel = false ;
                        boolean location = false ;
                        boolean audio = false ;
                        for(String s:permissions){
                            switch (s){
                                case Permission.WRITE_EXTERNAL_STORAGE:
                                case Permission.READ_EXTERNAL_STORAGE:
                                    io = true;
                                    break;
                                case Permission.RECORD_AUDIO:
                                    audio = true;
                                    break;
                                case Permission.CAMERA:
                                    camera =true;
                                    break;
                                case Permission.ACCESS_COARSE_LOCATION:
                                case Permission.ACCESS_FINE_LOCATION:
                                    location = true;
                                    break;
                                case Permission.CALL_PHONE:
                                case  Permission.READ_PHONE_STATE:
                                    tel = true;
                                    break;

                            }
                        }
                        String msg = "";
                        if(io){
                            msg += "[存储]";
                        }
                        if(camera){
                            msg+="[相机]";
                        }
                        if(audio){
                            msg+="[录音]";
                        }
                        if(tel){
                            msg+="[电话]";
                        }
                        if(location){
                            msg+="[您的位置]";
                        }
                        AlertDialog alertDialog = new AlertDialog(activity);
                        alertDialog.setTitle("系统未授权");
                        alertDialog.setContent("您未开启系统"+msg+"权限，将导致此应用中部分功能不可用！建议您去开启");
                        alertDialog.setSubmitTxt("去开启");
                        alertDialog.setCancelTxt("取消");
                        alertDialog.hideJihuo();
                        alertDialog.setOutsideTouchable(false);
                        alertDialog.setOnResult(new BaseDialog.OnResult() {
                            @Override
                            public void onActive() {

                            }

                            @Override
                            public void onYes() {
                                    Uri packageURI = Uri.parse("package:" + activity.getPackageName());
                                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    activity.startActivity(intent);
                                    NToast.longToast(activity,"请点开权限选项去开启授权");
                            }

                            @Override
                            public void onNo() {

                            }
                        });
                        alertDialog.show(0, Gravity.CENTER);
                    }
                }).start();
    }
    public interface AfterDo{
        void doIt();
    }
}
